package com.shuoyue.ycgk.ui.account;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.exception.NetErrorException;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;
import retrofit2.http.Field;

public interface AccountContract {
    class Model extends BaseModel {
        public Observable<BaseResult<UserInfo>> login(String phone, String password,
                                                      String deviceId,
                                                      String deviceName) {
            return RetrofitClient.getInstance().getApi().login(phone, password,
                    deviceId,
                    deviceName);
        }

        public Observable<BaseResult<UserInfo>> smslogin(String phone, String code,
                                                         String deviceId,
                                                         String deviceName) {
            return RetrofitClient.getInstance().getApi().loginSms(phone, code,
                    deviceId,
                    deviceName);
        }

        public Observable<BaseResult<UserInfo>> oneKeyLogin(String token) {
            return RetrofitClient.getInstance().getApi().oneKeyLogin(token);
        }

        public Observable<BaseResult<String>> regist(String phone, String password, String code) {
            return RetrofitClient.getInstance().getApi().regist(phone, password, code);
        }

        public Observable<BaseResult<String>> findBackPwd(String phone, String password, String code) {
            return RetrofitClient.getInstance().getApi().findBackPwd(phone, password, password, code);
        }

        public Observable<BaseResult<String>> updatePwd(String passwordold, String password) {
            return RetrofitClient.getInstance().getApi().updatePwd(passwordold, password, password);
        }

        public Observable<BaseResult<UserInfo>> wechatLogin(String openId, String accesstoken,
                                                            String deviceId,
                                                            String deviceName) {
            return RetrofitClient.getInstance().getApi().wechatLogin(openId, accesstoken,
                    deviceId,
                    deviceName);
        }
    }

    interface View extends BaseView {
        void loginSuc(UserInfo userInfo);

        void registSuc();

        void registFail(String msg);

        void resetSuc();

        void bindPhone(String openId, String accessToken);
    }

    class Presenter extends BasePresenter<View> {
        private Model model = new Model();

        public void login(String userName, String password,
                          String deviceId,
                          String deviceName) {
            apply(model.login(userName, password,
                    deviceId,
                    deviceName))
                    .subscribe(new ApiSubscriber<Optional<UserInfo>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<UserInfo> s) {
                            mView.loginSuc(s.getIncludeNull());
                        }

                    });
        }

        public void smslogin(String userName, String code,
                             String deviceId,
                             String deviceName) {
            apply(model.smslogin(userName, code,
                    deviceId,
                    deviceName))
                    .subscribe(new ApiSubscriber<Optional<UserInfo>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<UserInfo> s) {
                            mView.loginSuc(s.getIncludeNull());
                        }

                    });
        }

        public void oneKeyLogin(String token) {
            apply(model.oneKeyLogin(token))
                    .subscribe(new ApiSubscriber<Optional<UserInfo>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<UserInfo> s) {
                            mView.loginSuc(s.getIncludeNull());
                        }

                    });
        }

        public void wechatLogin(String openId, String accesstoken,
                                String deviceId,
                                String deviceName) {
            apply(model.wechatLogin(openId, accesstoken,
                    deviceId,
                    deviceName))
                    .subscribe(new ApiSubscriber<Optional<UserInfo>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<UserInfo> s) {
                            mView.loginSuc(s.getIncludeNull());
                        }

                        @Override
                        protected void onFail(NetErrorException error) {
                            if (error.getErrorType() == 2) {
                                mView.bindPhone(openId, accesstoken);
                            } else
                                super.onFail(error);
                        }
                    });
        }

        public void regist(String phone, String password, String code) {
            apply(model.regist(phone, password, code))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<String> stringOptional) {
                            super.onNext(stringOptional);
                            mView.registSuc();
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.registFail(e.getMessage());
                        }
                    });
        }

        public void reset(String phone, String password, String code) {
            apply(model.findBackPwd(phone, password, code))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<String> stringOptional) {
                            super.onNext(stringOptional);
                            mView.resetSuc();
                        }
                    });

        }

        public void updatePwd(String passwordold, String password) {
            apply(model.updatePwd(passwordold, password))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<String> stringOptional) {
                            super.onNext(stringOptional);
                            mView.resetSuc();
                        }
                    });

        }

    }


}
